#pragma once

#include <string>
#include <atomic>
#include <fstream>
#include <cstring>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#include <boost/algorithm/string.hpp>

namespace ns_util
{
    const std::string temp_path = "./temp/";
    class PathUtil
    {
    public:
        static std::string AddSuffix(const std::string &file_name, const std::string &suffix)
        {
            std::string path_name = temp_path;
            path_name += file_name;
            path_name += suffix;

            return path_name;
        }

        static std::string Src(const std::string &file_name)
        {
            return AddSuffix(file_name, ".cpp");
        }
        static std::string Exe(const std::string &file_name)
        {
            return AddSuffix(file_name, ".exe");
        }
        static std::string CompilerError(const std::string &file_name)
        {
            return AddSuffix(file_name, "compiler_err");
        }

        //
        static std::string Stdin(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stdin");
        }
        static std::string Stdout(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stdout");
        }
        static std::string Stderr(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stderr");
        }


    };

    class TimeUtil
    {
    public:
        //秒级时间戳
        static std::string GetTimeStamp()
        {
            timeval local;
            gettimeofday(&local, nullptr);
            return std::to_string(local.tv_sec);
        }
        //毫秒级时间戳
        static std::string GetTimeMsStamp()
        {
            timeval local;
            gettimeofday(&local, nullptr);
            return std::to_string(local.tv_sec * 1000 + local.tv_usec / 1000);
        }
    };

    class FileUtil
    {
    public:
        static bool IsFileExits(const std::string &path_name)
        {
            struct stat st;
            if (stat(path_name.c_str(), &st) == 0)
            {
                return true;
            }
            return false;
        }

        static std::string UniqFileName()
        {
            static std::atomic_int i(0);//原子性递增的计数器
            i++;
            std::string uniq_time = TimeUtil::GetTimeMsStamp();
            //我们使用毫秒级时间戳加上原子性递增的数来当作每个文件的唯一文件名
            return uniq_time + "_" + std::to_string(i);
        }

        static bool WriteFile(const std::string &file_name, std::string &code)
        {
            std::ofstream out(file_name);
            if(!out.is_open())
            {
                return false;
            }
            out.write(code.c_str(), code.size());
            out.close();
            return true;
        }

        static bool ReadFile(const std::string &file_name, std::string *des, bool keep_sep)
        {
            std::ifstream in(file_name);
            if(!in.is_open())
            {
                return false;
            }
            std::string line;
            while(std::getline(in, line))//getline按行读取并会去掉行分隔符（\n），所以我们根据keep_sep参数来决定是否要加上分隔符
            {
                *des += line;
                *des += keep_sep ? "\n" : " ";
            }
            in.close();
            return true;
        }
    };

    class StringUtil
    {
    public:
        static void SplitString(const std::string &str, std::vector<std::string> &tar, std::string sep)
        {
            boost::split(tar, str, boost::is_any_of(sep), boost::algorithm::token_compress_on);
        }
    };

}
